package cn.mandifal.search.service.impl;

import cn.mandifal.pojo.DpItem;
import cn.mandifal.search.service.SearchService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.List;
/**
 * 插入商品到 solr 库的 activemq 监听类
 * @author
 */
@Component
public class GoodsSearchListener implements MessageListener {
    @Autowired
    private SearchService searchService;

    @Override
    public void onMessage(Message message) {
        TextMessage textMessage= (TextMessage) message;
        try {
            String text = textMessage.getText();
            System.out.println("------------ 监听到消息");
            List<DpItem> dpItems = JSON.parseArray(text, DpItem.class);
            searchService.importList(dpItems);
            System.out.println("---------导入到 solr 索引库成功");
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
